<?php

require_once(WSB_MODULES_PATH .'sherpa/models/BaseGateway.php');
require_once(WSB_MODULES_PATH .'sherpa/models/ColorApplications.php');

class Sherpa_Model_ColorApplicationGateway extends Sherpa_Model_BaseGateway
{

	/**
	 * @param array|integer $parentProductId
	 * @return array|Sherpa_Model_ColorApplications
	 */
	public function fetchParentProductColorApplications($parentProductIds)
	{
		if(is_int($parentProductIds)) {
			$parentProductIds = array($parentProductIds);
		}
		if(empty($parentProductIds)) {
			return $this->useModels() ? new Sherpa_Model_ColorApplications(array()) : array();
		}
		$select = $this->getDbSelect();
		$select->from(array('ca' => 'sherpa_colorApplications'));
		$select->join(array('ptca' => 'sherpa_productToColorApplication'), 'ca.id = ptca.colorApplicationId', array('parentProductId'));
		$select->join(array('c1' => 'sherpa_colors'), 'c1.id = ca.primaryColorId', array('primaryColor' => 'colorName', 'primaryRgbRed' => 'rgbRed', 'primaryRgbGreen' => 'rgbGreen', 'primaryRgbBlue' => 'rgbBlue'));
		$select->joinLeft(array('c2' => 'sherpa_colors'), 'c2.id = ca.secondaryColorId', array('secondaryColor' => 'colorName', 'secondaryRgbRed' => 'rgbRed', 'secondaryRgbGreen' => 'rgbGreen', 'secondaryRgbBlue' => 'rgbBlue'));
		$select->where('ptca.parentProductId IN ( ? )', $parentProductIds);
		$select->order('ptca.sort ASC');
		$stmt = $select->query();
		$rows = $this->_refineRows($stmt->fetchAll());
		if($this->useModels()) {
			return new Sherpa_Model_ColorApplications($rows);
		} else {
			return $rows;
		}
	}

	/**
	 * @param array $childProductIds
	 * @return array
	 */
	public function fetchChildProductColorApplications(array $childProductIds)
	{
		if(empty($childProductIds)) {
			return $this->useModels() ? new Sherpa_Model_ColorApplications(array()) : array();
		}
		$select = $this->getDbSelect();
		$select->from(array('ca' => 'sherpa_colorApplications'));
		$select->join(array('p' => 'sherpa_products'), 'p.colorApplicationId = ca.id', array());
		$select->join(array('c1' => 'sherpa_colors'), 'c1.id = ca.primaryColorId', array('primaryColor' => 'colorName', 'primaryRgbRed' => 'rgbRed', 'primaryRgbGreen' => 'rgbGreen', 'primaryRgbBlue' => 'rgbBlue'));
		$select->joinLeft(array('c2' => 'sherpa_colors'), 'c2.id = ca.secondaryColorId', array('secondaryColor' => 'colorName', 'secondaryRgbRed' => 'rgbRed', 'secondaryRgbGreen' => 'rgbGreen', 'secondaryRgbBlue' => 'rgbBlue'));
		$select->where('p.id IN ( ? )', $childProductIds);
		$stmt = $select->query();
		$rows = $this->_refineRows($stmt->fetchAll());
		if($this->useModels()) {
			return new Sherpa_Model_ColorApplications($rows);
		} else {
			return $rows;
		}
	}

	/**
	 * @param array $rows
	 * @return array
	 */
	private function _refineRows(array $rows)
	{
		foreach($rows AS $index=>$t) {
			$colors = array();
			if(!empty($t['primaryColorId'])) {
				$colors[] = array(
					'id' => $t['primaryColorId'],
					'colorName' => $t['primaryColor'],
					'rgbRed' => $t['primaryRgbRed'],
					'rgbGreen' => $t['primaryRgbGreen'],
					'rgbBlue' => $t['primaryRgbBlue']
				);
			}
			if(!empty($t['secondaryColorId'])) {
				$colors[] = array(
					'id' => $t['secondaryColorId'],
					'colorName' => $t['secondaryColor'],
					'rgbRed' => $t['secondaryRgbRed'],
					'rgbGreen' => $t['secondaryRgbGreen'],
					'rgbBlue' => $t['secondaryRgbBlue']
				);
			}
			unset($rows[$index]['primaryColor']);
			unset($rows[$index]['primaryRgbRed']);
			unset($rows[$index]['primaryRgbGreen']);
			unset($rows[$index]['primaryRgbBlue']);
			unset($rows[$index]['secondaryColor']);
			unset($rows[$index]['secondaryRgbRed']);
			unset($rows[$index]['secondaryRgbGreen']);
			unset($rows[$index]['secondaryRgbBlue']);
			$rows[$index]['colors'] = $colors;
		}
		return $rows;
	}

}
